#include<iostream>
#include<ctemplate/template.h>
#include"oj_model.hpp"

static std::string html_path = "./questions_html/";

namespace oj_view
{
    using namespace oj_model;

    class View
    {
    public:
        void AllQuestionsHtml(std::vector<Question> &questions, std::string *html)
        {
            //使用ctemplate构建html并返回

            //1.形成路径
            std::string src_path = html_path + "all_questions.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            //由于不只一个题目，我们要循环构建子字典
            for(auto &it : questions)
            {
                ctemplate::TemplateDictionary *child = root.AddSectionDictionary("question_list");
                child -> SetValue("number", it._num);
                child -> SetValue("title", it._title);
                child -> SetValue("difficulty", it._difficulty);
            }
            //3.获取渲染对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_path, ctemplate::DO_NOT_STRIP);
            
            //4.添加字典数据到网页
            tpl->Expand(html, &root);
            
        }

        void OneQuestionHtml(Question &question, std::string *html)
        {
            //1.形成路径
            std::string src_path = html_path + "one_question.html";
            //2.形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", question._num);
            root.SetValue("title", question._title);
            root.SetValue("pre_code", question._header);
            root.SetValue("desc", question._desc);


            //3.获取被渲染对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_path, ctemplate::DO_NOT_STRIP);

            //4.添加字典数据到网页
            tpl->Expand(html, &root);
        }


    };
}